package com.wsoft.hr.pm.controller;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.wsoft.hr.pm.entity.HrPmTargetEntity;
import com.wsoft.hr.pm.query.target.TargetInsertQuery;
import com.wsoft.hr.pm.query.target.TargetListQuery;
import com.wsoft.hr.pm.query.target.TargetUpdateQuery;
import com.wsoft.hr.pm.service.IHrPmTargetService;
import com.wsoft.hr.pm.vo.target.TargetVO;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 指标库 前端控制器
 *
 * @author Z001
 * @since 2025年02月17日 15:06:35
 */
@RestController
@Tag(name = "个人指标")
@RequestMapping("/hr/pm/target")
public class HrPmPersonTargetController {

    @Resource
    private IHrPmTargetService targetService;

    @Operation(summary = "个人指标详情", description = "指标详情")
    @GetMapping("/personGet")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(targetService.getPersonTargetInfo(id));
    }

    @Operation(summary = "个人指标分页", description = "个人指标分页")
    @PostMapping("/listPersonEnable")
    public ResponseResult listPersonEnable(@RequestBody TargetListQuery query) {
        query.setEnableFlag(1);
        PageInfo page = targetService.listTarget(query, true);
        List<HrPmTargetEntity> list = page.getList();
        List<TargetVO> resultList = new ArrayList<>();
        for (HrPmTargetEntity targetEntity : list) {
            TargetVO targetVO = Convert.convert(TargetVO.class, targetEntity);
            targetVO.setUpdateWeight(true);
            targetVO.setUpdateStandard(true);
            targetVO.setUpdateParTargetId(true);
            targetVO.setUpdateRemark(true);
            targetVO.setUpdateGroupId(true);
            resultList.add(targetVO);
        }
        page.setList(resultList);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "个人指标新增", description = "个人指标新增")
    @PostMapping("/personTargetInsert")
    public ResponseResult personTargetInsert(@RequestBody @Valid TargetInsertQuery query) {
        HrPmTargetEntity entity = targetService.personTargetInsert(query);
        TargetVO targetVO = Convert.convert(TargetVO.class, entity);
        targetVO.setUpdateWeight(true);
        targetVO.setUpdateStandard(true);
        targetVO.setUpdateParTargetId(true);
        targetVO.setUpdateRemark(true);
        targetVO.setUpdateGroupId(true);
        return ResponseResult.success(targetVO);
    }

    @Operation(summary = "个人指标修改", description = "个人指标修改")
    @PostMapping("/personTargetUpdate")
    public ResponseResult personTargetUpdate(@RequestBody @Valid TargetUpdateQuery query) {
        boolean b = targetService.personTargetUpdate(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "个人指标删除", description = "个人指标删除")
    @GetMapping("/personTargetDelete")
    public ResponseResult personTargetDelete(@RequestParam("id") Long id) {
        boolean b = targetService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

}
